Completed
Pull Request — master (#238)
by Olivier
02:39
created

FileList.getUploadUrl   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 5
rs 9.4285
1
/* global _, Gallery, Thumbnails */
2
/**
3
 * OCA.FileList methods needed for file uploading
4
 *
5
 * This hack makes it possible to use the Files scripts as is, without having to import and
6
 * maintain them in Gallery
7
 *
8
 * Empty methods are for the "new" button, if we want to implement that one day
9
 *
10
 * @type {{findFile: FileList.findFile, createFile: FileList.createFile,
11
 *     getCurrentDirectory: FileList.getCurrentDirectory, getUploadUrl:
12
 *     FileList.getUploadUrl}}
13
 */
14
var FileList = {
0 ignored issues
show
Bug introduced by
Redefinition of 'FileList'.
Loading history...
15
	/**
16
	 * Makes sure the filename does not exist
17
	 *
18
	 * Gives an early chance to the user to abort the action, before uploading everything to the
19
	 * server.
20
	 * Albums are not supported as we don't have a full list of images contained in a sub-album
21
	 *
22
	 * @param fileName
23
	 * @returns {*}
24
	 */
25
	findFile: function (fileName) {
26
		"use strict";
27
		var path = Gallery.currentAlbum + '/' + fileName;
28
		var galleryImage = Gallery.imageMap[path];
29
		if (galleryImage) {
30
			var fileInfo = {
31
				name: fileName,
32
				directory: Gallery.currentAlbum,
33
				path: path,
34
				etag: galleryImage.etag,
35
				mtime: galleryImage.mTime * 1000, // Javascript gives the Epoch time in milliseconds
36
				size: galleryImage.size
37
			};
38
			return fileInfo;
39
		} else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
40
			return null;
41
		}
42
	},
43
44
	/**
45
	 * Create an empty file inside the current album.
46
	 *
47
	 * @param {string} name name of the file
48
	 *
49
	 * @return {Promise} promise that will be resolved after the
50
	 * file was created
51
	 *
52
	 */
53
	createFile: function(name) {
54
		var self = this;
55
		var deferred = $.Deferred();
56
		var promise = deferred.promise();
57
58
		OCA.Files.isFileNameValid(name);
59
60
		var targetPath = this.getCurrentDirectory() + '/' + name;
61
62
		//Check if file already exists
63
		if(Gallery.imageMap[targetPath]) {
64
			OC.Notification.showTemporary(
65
				t('files', 'Could not create file "{file}" because it already exists', {file: name})
66
			);
67
			deferred.reject();
68
			return promise;
69
		}
70
71
		Gallery.filesClient.putFileContents(
72
			targetPath,
73
			'',
74
			{
75
				contentType: 'text/plain',
76
				overwrite: true
77
			}
78
			)
79
			.done(function() {
80
				// TODO: error handling / conflicts
81
				Gallery.filesClient.getFileInfo(
82
					targetPath, {
83
						properties: self.findFile(targetPath)
84
					}
85
					)
86
					.then(function(status, data) {
87
						deferred.resolve(status, data);
88
					})
89
					.fail(function(status) {
90
						OC.Notification.showTemporary(t('files', 'Could not create file "{file}"', {file: name}));
91
						deferred.reject(status);
92
					});
93
			})
94
			.fail(function(status) {
95
				if (status === 412) {
96
					OC.Notification.showTemporary(
97
						t('files', 'Could not create file "{file}" because it already exists', {file: name})
98
					);
99
				} else {
100
					OC.Notification.showTemporary(t('files', 'Could not create file "{file}"', {file: name}));
101
				}
102
				deferred.reject(status);
103
			});
104
105
		return promise;
106
	},
107
108
109
	/**
110
	 * Retrieves the current album
111
	 *
112
	 * @returns {string}
113
	 */
114
	getCurrentDirectory: function () {
115
		"use strict";
116
117
		// In Files, dirs start with a /
118
		return '/' + Gallery.currentAlbum;
119
	},
120
121
	/**
122
	 * Retrieves the WebDAV upload URL
123
	 *
124
	 * @param {string} fileName
125
	 * @param {string} dir
126
	 *
127
	 * @returns {string}
128
	 */
129
	getUploadUrl: function (fileName, dir) {
130
		if (_.isUndefined(dir)) {
131
			dir = this.getCurrentDirectory();
132
		}
133
134
		var pathSections = dir.split('/');
135
		if (!_.isUndefined(fileName)) {
136
			pathSections.push(fileName);
137
		}
138
		var encodedPath = '';
139
		_.each(pathSections, function (section) {
140
			if (section !== '') {
141
				encodedPath += '/' + encodeURIComponent(section);
142
			}
143
		});
144
		return OC.linkToRemoteBase('webdav') + encodedPath;
145
	}
146
};
147
148
/**
149
 * OCA.Files methods needed for file uploading
150
 *
151
 * This hack makes it possible to use the Files scripts as is, without having to import and
152
 * maintain them in Gallery
153
 *
154
 * @type {{isFileNameValid: Files.isFileNameValid, generatePreviewUrl: Files.generatePreviewUrl}}
155
 */
156
var Files = {
157
	App: {fileList: {}},
158
159
	isFileNameValid: function (name) {
160
		"use strict";
161
		var trimmedName = name.trim();
162
		if (trimmedName === '.' || trimmedName === '..') {
163
			throw t('files', '"{name}" is an invalid file name.', {name: name});
164
		} else if (trimmedName.length === 0) {
165
			throw t('files', 'File name cannot be empty.');
166
		}
167
		return true;
168
169
	},
170
171
	/**
172
	 * Generates a preview for the conflict dialogue
173
	 *
174
	 * Since Gallery uses the fileId and Files uses the path, we have to use the preview endpoint
175
	 * of Files
176
	 */
177
	generatePreviewUrl: function (urlSpec) {
178
		"use strict";
179
		var previewUrl;
180
		var path = urlSpec.file;
181
182
		// In Files, root files start with //
183
		if (path.indexOf('//') === 0) {
184
			path = path.substring(2);
185
		} else {
186
			// Directories start with /
187
			path = path.substring(1);
188
		}
189
190
		if (Gallery.imageMap[path]) {
191
			var fileId = Gallery.imageMap[path].fileId;
192
			var thumbnail = Thumbnails.map[fileId];
193
			previewUrl = thumbnail.image.src;
194
		} else {
195
			var previewDimension = 96;
196
			urlSpec.x = Math.ceil(previewDimension * window.devicePixelRatio);
197
			urlSpec.y = Math.ceil(previewDimension * window.devicePixelRatio);
198
			urlSpec.forceIcon = 0;
199
			previewUrl = OC.generateUrl('/core/preview.png?') + $.param(urlSpec);
200
		}
201
202
		return previewUrl;
203
	}
204
};
205
206
OCA.Files = Files;
207
OCA.Files.App.fileList = FileList;
208